package indi.zn.commons.logger;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Objects;

/**
 * @ProjectName:Dupslog
 * @Description: 
 * @author: 周楠
 * @date 2017年4月11日 下午1:37:00
 */

public class LoggerReader {

	private  BufferedReader reader;
	private int linePoint=-1;

	public LoggerReader(String path){
	File file =new File(path);
		
	FileInputStream fis=null;
	try {
		fis = new FileInputStream(file);
	} catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}   
	InputStreamReader isr = new InputStreamReader(fis);   
	reader= new BufferedReader(isr);  

	}
	
    public LoggerReader(InputStream is){
		
    	 InputStreamReader isr = new InputStreamReader(is);   
    	 reader= new BufferedReader(isr);  
	}
	
   
    public Integer getLinePoint() {
		return linePoint;
	}

	private void plusPoint(){
    	linePoint++;   	
    }
	
	public void reset() throws IOException{
		reader.reset();
		linePoint=-1;
	}
    
     public String readnextLine() throws IOException{
    	 
    	String linestr= reader.readLine();
    	 
    	 plusPoint();
    	 
    	 return linestr;
   
      }
     
     public void skipLines(int lines){
    	 
    	 for (int i = 0; i < lines; i++) {
    		 try {
				readnextLine();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}  		 
		}
    	 
     }
     
     
     
    /**
     * 读取1行并进行操作
     * @MethodName:readAtLineAndDo
     * @author: 姓名
     * @date 2017年4月11日 下午2:01:17
     * @param line      0 开始
     * @param incident
     * @param params
     * @return
     * @throws IOException
     */
     public Integer readAtLineAndDo(int line,ReadLineIncident incident,Object...params) throws IOException{
	   	 
    	 String linestr=null;
    	while((linestr = readnextLine()) != null){
    	  
    	  if(Objects.equals(linePoint, line)){
    		  
    		  incident.doLine(new String[]{linestr}, params);
    		  
    		  break;
    	  }
    	}
    	  return linePoint;
      }
     
 /**
  *  读取连续的行 并进行操作
  * @MethodName:readContinuousLineAndDo
  * @author: 姓名
  * @date 2017年4月11日 下午2:00:47
  * @param start   0 开始
  * @param count   1 开始 
  * @param incident
  * @param params
  * @return
  * @throws IOException
  */
    public Integer readContinuousLineAndDo(int start,int count ,ReadLineIncident incident,Object...params) throws IOException{
	     	 
    	String[] lines= new String[count];
    	
    	int i=0;
    	
    	boolean flag=false;
    	
     	while((lines[i] = readnextLine()) != null){
        
     	if(Objects.equals(linePoint, start)){
     		flag=true;
   	
     	}

      if(flag=true){	
     	if(i<count-1){
         i++;
     	}else{
     	 break;
     	}
      }
     	
     	}

     if(flag){
     	  incident.doLine(lines, params);
     }
     
    	  return linePoint;
      }
     
	
}
